package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.user.AddGoodsEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.NewCategeryEntity;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.publish.AddGoodsActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddGoodsModel extends BaseViewModel {
    public ObservableField<AddGoodsEntity> addGoodsEntity = new ObservableField<>(new AddGoodsEntity());
    private AddGoodsActivity activity;
    private SelfRepertory repertory;

    public AddGoodsModel(AddGoodsActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    //新增
    public void addGoodsModel() {
        DialogHelper.showProgressDialog(activity, null, "数据上传中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().addGoods(addGoodsEntity.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.showSuccess();
                }else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //编辑商品
    public void editGoodsModel() {
        DialogHelper.showProgressDialog(activity, null, "数据上传中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().editGoods(addGoodsEntity.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.showSuccess();
                }else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getGoodsById(String id){
        DialogHelper.showProgressDialog(activity, null, "数据上传中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getGoodsById(id).enqueue(new Callback<BaseEntity<AddGoodsEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<AddGoodsEntity>> call, Response<BaseEntity<AddGoodsEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.showEditeInfo(response.body().getData());
                    //addGoodsEntity.set(response.body().getData());
                }else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<AddGoodsEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //上传多图片
    public void uploadMutiFileModel(File file, int type, int completeNum) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + completeNum + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, null, "图片上传中"+completeNum+"/"+activity.imgTotal+"...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.uploadRefresh(response.body().getData().toString(), type);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });

        /*DialogHelper.showProgressDialog(activity, null, "图片上传中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.uploadMutiFile(fileList)
                .subscribe(new Consumer<BaseEntity<List<String>>>() {
                    @Override
                    public void accept(BaseEntity<List<String>> baseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        activity.uploadImgSuccess(baseEntity.getData(), type);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });*/
    }

    //获取新分类数据
    public void getCategeryModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getNewCategoryMenu()
                .subscribe(new Consumer<BaseEntity<List<NewCategeryEntity<List<NewCategeryEntity<List<NewCategeryEntity>>>>>>>() {
                    @Override
                    public void accept(BaseEntity<List<NewCategeryEntity<List<NewCategeryEntity<List<NewCategeryEntity>>>>>> listBaseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        if (listBaseEntity.getCode().equals(APIManager.REQUEST_SUCCESS)) {
                            if (listBaseEntity.getData() != null && listBaseEntity.getData().size() > 0) {
                                activity.showNewCategoryLeftView(listBaseEntity.getData());
                            }
                        }else {
                            MToast.showToast(activity, listBaseEntity.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                    }
                });
    }

    //获取价格单位
    public void getPriceUnitModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().selectUnit().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.showUnitPop(response.body().getData());
                }else {
                    MToast.showToast(activity,response.body().getMsg() );
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取地址数据
    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                if (response.body().getCode().equals("200")){
                    DialogHelper.dismissProgressDialog();
                    activity.showAddressPop(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
